<?php

namespace Api\Member\ApiPass\Open;

use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;

/**
 * 控制器
 */
class Controller extends BaseController
{
    /**
     * 构造方法
     */
    public function __construct(
        ApiPassRepository $ApiPassRepository
    ) {
        $this->ApiPassRepository = $ApiPassRepository;
    }

    /**
     *
     * @api {get} /member/commit 提款接口
     * @apiName commit
     * @apiGroup ApiPass\Open
     *
     * @apiParam  {String}  account                     商户号
     * @apiParam  {String}  business                    交易方
     * @apiParam  {String}  businessBank                对方银行名称
     * @apiParam  {String}  businessCard                交易方银行卡号
     * @apiParam  {String}  businessDescription         订单备注
     * @apiParam  {String}  businessPhone               手机号
     * @apiParam  {String}  money                       金额
     * @apiParam  {String}  notifyUrl                   通知地址
     * @apiParam  {String}  reverseUrl                  回调地址
     * @apiParam  {String}  shOrderId                   第三方商户订单号
     * 
     * @apiSuccess {String} code                        请求状态码
     * @apiSuccess {String} message                     请求返回信息
     * @apiSuccess {String} status                      请求状态
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     *     "code": 100000,
     *     "message": "成功",
     *     "status": true,
     *     "data": []
     * }
     */

    public function commit(Request $request, array $headers = [])
    {
        try {
            $account             = $request->get('account');
            $business            = $request->get('business');
            $businessBank        = $request->get('businessBank');
            $businessCard        = $request->get('businessCard');
            $businessDescription = $request->get('businessDescription');
            $businessPhone       = $request->get('businessPhone');
            $money               = $request->get('money');
            $notifyUrl           = $request->get('notifyUrl');
            $reverseUrl          = $request->get('reverseUrl');
            $shOrderId           = $request->get('shOrderId');
            // 提交的变量格式判断
            $this->ApiPassRepository->judgment($account, $business, $businessBank, $businessCard, $businessDescription, $businessPhone, $money, $notifyUrl, $reverseUrl, $shOrderId);
            // 验证数据
            $result = $this->ApiPassRepository->commit($account, $business, $businessBank, $businessCard, $businessDescription, $businessPhone, $money, $notifyUrl, $reverseUrl, $shOrderId);
            // 返回成功信息
            success($result);            
        } catch (\Sms\Exceptions\Exception $e) {
            error('validation_fail', $e->getMessage());
        } catch (\Member\ApiPass\Exceptions\Exception $e) {
            error('validation_fail', $e->getMessage());
        }
    }
    
    public function query(Request $request)
    {
        $list = $this->ApiPassRepository->query();
        success(compact('list'));
    }
}
